#!/usr/bin/python
import sys
import MySQLdb
import math

conn = MySQLdb.connect (host = "giza",
                           user = "visquick_rw",
                           passwd = "r34dwr1t3",
                           db = "tcga")
cursor = conn.cursor ()
#update coad_read_features set patient_values = '.1:.2:.3', patient_values_mean = .234 where alias = 'N:METH:cg00000292:chr16:28797601::'
args = sys.argv
table = "coad_read_features_c"
infile = 'crad_imputed_hg18coords_ranks_finalC.top80.16feb11.tsv'
label = ''
if (len(args) >= 3):
        table = args[1]
        infile = args[2]
        if (len(args) == 4):
                label = args[3]
else:
        print ("usage is py2.6 parse_features.py tablename infile optional [label(ie mirna)]")

def execCradSql(updateSql):
        #print updateSql
        cursor.execute(updateSql)
        print "sql %s updated: %d" % (updateSql, cursor.rowcount)

fin = open(infile,'r')
fout = open('top80C_ranks.tsv','w')
#sqlout = open('top80C_ranks.sql','w')
#update coad_read_features_u set rank = 0, weight = 0 where alias = 'N:CNVR::chr13:71375000:71378999:'
lines = fin.readlines()
fin.close()
for line in lines:
	columns = line.split('\t') #col[0] and col[1]
	fields=line.strip().replace('\t',':').replace(':','\t')
	data = fields.split('\t')
	if len(data) > 4 and len(data[3]) > 3:
		data[3] = data[3][3:]		
	fout.write("\t".join(data) + "\n")
	execCradSql("update " + table + " set rank = " + columns[1] + " where alias ='" + columns[0] + "'")

fout.close()

